package com.burgeon.backend.SHOP.service.Impl;

import com.burgeon.backend.SHOP.entity.VipEmployeeVo;
import com.burgeon.backend.SHOP.mappers.AppUserMapper;
import com.burgeon.backend.SHOP.rest.*;
import com.burgeon.backend.SHOP.service.AppChatService;
import com.burgeon.backend.SHOPBACKEND.entity.Employee;
import com.burgeon.backend.entity.Info;
import com.burgeon.backend.entity.User;
import com.burgeon.backend.mappers.AccountMapper;
import com.burgeon.backend.service.ex.UploadException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;

@Service
public class AppChatServiceImpl implements AppChatService {
    private final static Log logger = LogFactory.getLog(AppChatService.class);
    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private AppUserMapper appUserMapper;

    /**
     * 根据小程序openid判断是否有绑定导购
     * @param openId
     * @return
     */
    @Override
    public VipEmployeeVo IsEmpByOpenId(String openId, String token) {
        Info info = GetInfo(token);
        findEmployeeByOpenId findEmployeeByOpenId = new findEmployeeByOpenId(info.getUrl(), info.getAccount(), info.getPassword());
        FindEmpDataByStoreId findEmpDataByStoreId = new FindEmpDataByStoreId(info.getUrl(), info.getAccount(), info.getPassword());
        try {
            Employee employee = findEmployeeByOpenId.getData(openId);
            VipEmployeeVo vipEmployeeVo = new VipEmployeeVo();
            //是否有导购信息

            if (null==employee){
                VipEmployeeVo vipEmployeeVo1 = new VipEmployeeVo();
                vipEmployeeVo1.setIsHaveEmployee("N");
                return vipEmployeeVo1;
            }
            if("null".equals(employee.getId())){
                System.err.println("么有导购");
                vipEmployeeVo.setIsHaveEmployee("N");
                //查看所属店仓的导购信息，头像，姓名，性别，电话
                //从erp查询店仓导购的信息
                ArrayList<Employee> employees =findEmpDataByStoreId.getData(employee.getStoreId());
                for (Employee emp:employees) {
                    //查询员工的头像
                    String img = appUserMapper.findData(emp.getId(),token);
                    emp.setImg(img);
                    emp.setStore(employee.getStore());
                }
                vipEmployeeVo.setEmployees(employees);
                logger.info(vipEmployeeVo);
            }else {
                vipEmployeeVo.setIsHaveEmployee("Y");
                vipEmployeeVo.setEmployee(employee);
            }
            logger.info("所属店仓的导购信息"+vipEmployeeVo);
            return vipEmployeeVo;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 选择绑定导购
     * @return
     */
    @Override
    public HashMap ChooseEmp(String openId,String id,String token) {
        if (null == id || id.isEmpty()){
            throw new UploadException("没有传导购的id");
        }
        logger.info("导购的id="+id);
        Info info = GetInfo(token);
        FindStoreByEmp findStoreByEmp = new FindStoreByEmp(info.getUrl(), info.getAccount(), info.getPassword());
        ModVipEmp modVipEmp = new ModVipEmp(info.getUrl(), info.getAccount(), info.getPassword());
        GETUserByOpenid getUserByOpenid = new GETUserByOpenid(info.getUrl(), info.getAccount(), info.getPassword());

        try {
            //根据openid查询VIP的id
            User user = getUserByOpenid.getData(openId);
            //根据导购的id查询导购的erp信息
            HashMap<String,String> emp = findStoreByEmp.getData(id);
            //修改VIP的服务导购，VIP的id和导购的编号
            modVipEmp.getData(user.getId(),emp.get("NO"),null);
            //并查询头像
            String img = appUserMapper.findData(id, token);
            emp.put("img", img);
            //是否导购
            emp.put("isEmp", user.getIsEmp());
            logger.info("绑定导购的信息"+emp);
            return emp;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
        }

        return null;
    }

    private Info GetInfo(String token) {
        return accountMapper.GetTokenData(token);
    }
}
